package com.bioroadlab.action;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.bioroadlab.service.DiseaseService;
import com.bioroadlab.service.SampleInfoService;
import com.bioroadlab.util.SpringUtils;

public class CreateIndexListener implements ServletContextListener, Runnable {
	private SessionFactory sessionFactory;
	private DiseaseService diseaseService;
	private SampleInfoService sampleInfoService;

	public void contextDestroyed(ServletContextEvent servletContextEvent) {
		
	}

	public void init(ServletContext servletContext) {
		sessionFactory = (SessionFactory) SpringUtils.getBean(servletContext,
				SessionFactory.class);
		diseaseService = (DiseaseService) SpringUtils.getBean(servletContext,
				DiseaseService.class);
		sampleInfoService = (SampleInfoService) SpringUtils.getBean(
				servletContext, SampleInfoService.class);
	}

	public void contextInitialized(ServletContextEvent servletContextEvent) {
		init(servletContextEvent.getServletContext());
		run();
	}

	public void run() {
		try {
			diseaseService.getIndexFile().indexFileForQuickSearch();

		} catch (IOException e) {
			e.printStackTrace();
		} finally {
		}

	}

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public DiseaseService getDiseaseService() {
		return diseaseService;
	}

	@Autowired
	public void setDiseaseService(DiseaseService diseaseService) {
		this.diseaseService = diseaseService;
	}

	public SampleInfoService getSampleInfoService() {
		return sampleInfoService;
	}

	@Autowired
	public void setSampleInfoService(SampleInfoService sampleInfoService) {
		this.sampleInfoService = sampleInfoService;
	}

}
